Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the simpleBuffer as implementation of the httputil.BufferPool. #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hiyosi
Copy link

@hiyosi hiyosi commented Sep 12, 2024

Description

There are some bugs in the buffer pool implementation of the infra package.

  1. Unnecessary clean slice.

b.pool.Put(buf[:0])

The copyBuffer function in the httputil.ReverseProxy package checks the length of the slice that given as argument(buf), and if the length of the slice is 0, it makes a slice as 32 * 1024 (32KB).
https://github.com/golang/go/blob/69234ded30614a471c35cef5d87b0e0d3c136cd9/src/net/http/httputil/reverseproxy.go#L642
The authorisation-proxy (infra package) always reset the slice length to 0 before put it to the pool, this means that the buffer size is always 32KB when copying.

  1. Incorrect comparison conditions.

if bufLen >= size || bufCap >= size {

Even if a slice with the same capacity as buffer.size is put, a new slice will be created.
Since httputil.ReverseProxy does not change the slice size, results in memory allocation every time a response is copied.
The impact of this issue is limited, you probably can't notice it if you don't specify a large value for bufferSize (see result of memory usage).

In this PR, a new simpleBuffer is introduced to solve the above problem.
The current implementation of BufferPool is overkill because the httputil.Reverseproxy does not resize the slice used to copy the response.
However the current implementation is still exists, it could be used for other purposes.

e.g. memory usage (origin server returns small response)

before

bufferSize requests/sec duration max rss (kb)
419430400 1/s 1s 440288
419430400 1/s 10s 1669664
419430400 100/s 1s 18902020
419430400 100/s 10s 27421440

after

bufferSize requests/sec duration max rss (kb)
419430400 1/s 1s 435296
419430400 1/s 10s 441516
419430400 100/s 1s 3351424
419430400 100/s 10s 6216764

Type of change

  • Bug fix
  • New feature
  • Refactoring (no functional changes, no api changes)
  • Non-code changes (update documentation, pipeline, etc.)

Flags

  • Breaks backward compatibility
  • Requires a documentation update
  • Has untestable code

Related issue/PR

Delete this section if there are no issues or pull requests that relate to this pull request.

  • Fixes #issue
  • Closes #PR

Checklist

  • Followed the guidelines in the CONTRIBUTING document
  • Added prefix [skip ci]/[ci skip]/[no ci]/[skip actions]/[actions skip] in the PR title if necessary
  • Tested and linted the code
  • Commented the code
  • Made corresponding changes to the documentation
  • Passed all pipeline checking

Checklist for maintainer

  • Use Squash and merge
  • Double-confirm the merge message has prefix [skip ci]/[ci skip]/[no ci]/[skip actions]/[actions skip]
  • Delete the branch after merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant